package xml;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Vector;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.input.SAXBuilder;

import dbStruct.Column;
import dbStruct.Connector;
import dbStruct.Connectors;
import dbStruct.DatabaseStructure;
import dbStruct.Nodes;
import dbStruct.Table;
import dbStruct.Connector.Cardinality;
import dbStruct.Connector.RelationType;

public class XMLReader {
	private File 				inputFile;
	private DatabaseStructure 	dbStruct;
	private Nodes				nodes;
	private Connectors			connectors;
	
	public XMLReader()
	{
		super();
		dbStruct=DatabaseStructure.getInstance();
		nodes=dbStruct.getNodes();
		connectors=dbStruct.getConnectors();
	}
	
	public XMLReader(File inputFile)
	{
		super();
		this.inputFile=inputFile;
		dbStruct=DatabaseStructure.getInstance();
		nodes=dbStruct.getNodes();
		connectors=dbStruct.getConnectors();
	}
	
	public void setInputFile(File inputFile)
	{
		this.inputFile=inputFile;
	}
	
	@SuppressWarnings("unchecked")
	public DatabaseStructure read()
	{	
		SAXBuilder builder = new SAXBuilder();
		nodes.clear();
		connectors.clear();
		try {
			Document xml = builder.build(this.inputFile);
			Element root = xml.getRootElement();
			List<Element> children = root.getChildren("TABLE");
			for (int i=0;i<children.size();i++)
			{	//Leggo la tabella dal file XML
				Element tblChild = children.get(i);
				Table tableNode = new Table(tblChild.getAttributeValue("NAME"));
				nodes.add(tableNode);
				
				List<Element> colChildren = tblChild.getChildren("COLUMN");
				for (int c=0;c<colChildren.size();c++)
				{	//Leggo una colonna della tabella
					Element colChild = colChildren.get(c);
					Column column = new Column(colChild.getAttributeValue("NAME"));
					     column.setIndex(Boolean.parseBoolean(colChild.getAttributeValue("INDEX")));
					     column.setPrimary(Boolean.parseBoolean(colChild.getAttributeValue("PK")));
					     column.setOrdinalPosition(Integer.parseInt(colChild.getAttributeValue("POSITION")));
					     column.setDim(Integer.parseInt(colChild.getAttributeValue("DIMENSION")));
					     column.setColumnType(Integer.parseInt(colChild.getAttributeValue("TYPE")));
					nodes.add(column);
					//Aggiungo la relazione tabella->attributo
					Connector conn = new Connector(tableNode, column, RelationType.ATTRIBUTE, Cardinality.ONE2ONE);
					connectors.add(conn);
				}
			}
			
			List<Element> relChildren = root.getChildren("RELATION");
			System.out.println("XML READER, RELATIONS " + relChildren.size());
			for (int c=0;c<relChildren.size();c++)
			{
				Element relChild = relChildren.get(c);
				String relType   = relChild.getAttributeValue("TYPE");
				String relName   = relChild.getAttributeValue("NAME");
				
				Element pkRelation = relChild.getChild("PK_RELATION");
				Element fkRelation = relChild.getChild("FK_RELATION");
				
				String pkTableName = pkRelation.getAttributeValue("TABLE");
				String fkTableName = fkRelation.getAttributeValue("TABLE");
				
				Table pkTableNode  = (Table) nodes.get(pkTableName);
				Table fkTableNode  = (Table) nodes.get(fkTableName);
				//Aggiungo la relazione alla collezione CONNECTORS
				Connector conn = new Connector(pkTableNode, fkTableNode, RelationType.RELATION, Cardinality.valueOf(relType));
				connectors.add(conn);
			}
			
		} catch (JDOMException e) {
			System.out.println("JDOM EXCEPTION");
			e.printStackTrace();
		} catch (IOException e) {
			System.out.println("IO EXCEPTION");
			e.printStackTrace();
		}
		return dbStruct;
	}
	
}
